package com.annoncesautomobile.vue.annuairepro;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.concreteEntities.Departement;
import com.annoncesautomobile.modele.util.IModelXML;
import com.annoncesautomobile.modele.util.ModelXML;
import com.annoncesautomobile.vue.SuperActivity;
import com.annoncesautomobile.vue.adapters.ListeBoutonBas;

public class AnnuaireProChoixDepartementActivity extends SuperActivity {
	
	private final String EXTRA_IDDEPARTEMENT = "idDepartement";
	
	private IModelXML model;
	private List<Departement> listeDepartements;
	private Departement departementTousDepartements;
	
	private ArrayAdapter<Departement> adapter;
	
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_annuaire_pro_choix_departement);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
		TextView textViewActivityTitleText = (TextView) findViewById(R.id.activity_title_text);
		textViewActivityTitleText.setText(getString(R.string.title_activity_annuaire_pro_choix_departement));
		
		ListeBoutonBas listeBoutonBas = new ListeBoutonBas(this);
		LinearLayout lt = (LinearLayout) this.findViewById(R.id.mainLayoutAnnuaireProChoixDepartement);
		lt.addView(listeBoutonBas.getView());
		
		this.model = ModelXML.getInstance();
		this.departementTousDepartements = new Departement("","","Tous departements");
		this.listeDepartements = new ArrayList<Departement>();
		this.listeDepartements.addAll(this.model.getDepartements());
		this.listeDepartements.add(0,this.departementTousDepartements);
		
		this.adapter = new ArrayAdapter<Departement>(this, R.layout.list_item_fleche, R.id.text, this.listeDepartements);
		
		this.listView = (ListView) this.findViewById(R.id.listViewAnnuaireProChoixDepartement);
		
		LayoutInflater inflater = getLayoutInflater();
		LinearLayout header = (LinearLayout) inflater.inflate(R.layout.list_row_header_image_annuaire, this.listView, false);
		this.listView.addHeaderView(header, null, false);
		this.listView.addFooterView(new View(this), null, false);
		
		this.listView.setAdapter(this.adapter);
		
		this.listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(AnnuaireProChoixDepartementActivity.this, AnnuaireProActivity.class);
				intent.putExtra(EXTRA_IDDEPARTEMENT, AnnuaireProChoixDepartementActivity.this.adapter.getItem(position-1).getIdDep());
				startActivityForResult(intent, 0);
				overridePendingTransition(R.anim.aun, R.anim.adeux);
			}
		});
	}
	
	public void onBackPressed(View view) {
		this.onBackPressed();
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) 
	{
		overridePendingTransition(R.anim.animationleftright, R.anim.animationrightleft);
	}

}
